package ezXpns.GUI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Cursor;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.UUID;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import com.toedter.calendar.JDateChooser;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author A0086931X
 */

//@author A0086931X
public class BudgetItem extends JPanel {
	/**
	 * Create the panel.
	 */
	public BudgetItem() {
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 24, 122, 7, 45, 150, 80, 70, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 28, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);

		expenseNum = new JLabel(".");
		expenseNum.setText("");
		expenseNum.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_expenseNum = new GridBagConstraints();
		gbc_expenseNum.insets = new Insets(0, 0, 0, 5);
		gbc_expenseNum.gridx = 0;
		gbc_expenseNum.gridy = 0;
		add(expenseNum, gbc_expenseNum);

		txtName = new JTextField();
		txtName.setForeground(Color.LIGHT_GRAY);
		txtName.setText("name ");
		GridBagConstraints gbc_txtName = new GridBagConstraints();
		gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtName.anchor = GridBagConstraints.NORTH;
		gbc_txtName.insets = new Insets(0, 0, 0, 5);
		gbc_txtName.gridx = 1;
		gbc_txtName.gridy = 0;
		add(txtName, gbc_txtName);
		txtName.setColumns(10);
		txtName.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				txtName.setForeground(Color.BLACK);
				if (txtName.getText().equals("name ")) {
					txtName.setText("");
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				if (txtName.getText().equals("")) {
					txtName.setForeground(Color.LIGHT_GRAY);
					txtName.setText("name ");
				}
			}
		});
		txtName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					txtName.transferFocus();
				}
			}
		});

		JLabel label = new JLabel("$");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 0, 5);
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.gridx = 2;
		gbc_label.gridy = 0;
		add(label, gbc_label);

		txtAmount = new JTextField();
		txtAmount.setForeground(Color.LIGHT_GRAY);
		txtAmount.setText("price");
		GridBagConstraints gbc_txtAmount = new GridBagConstraints();
		gbc_txtAmount.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtAmount.anchor = GridBagConstraints.NORTH;
		gbc_txtAmount.insets = new Insets(0, 0, 0, 5);
		gbc_txtAmount.gridx = 3;
		gbc_txtAmount.gridy = 0;
		add(txtAmount, gbc_txtAmount);
		txtAmount.setColumns(6);
		txtAmount.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				txtAmount.setForeground(Color.BLACK);
				if (txtAmount.getText().equals("price")) {
					txtAmount.setText("");
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				if (txtAmount.getText().equals("")) {
					txtAmount.setForeground(Color.LIGHT_GRAY);
					txtAmount.setText("price");
				}
			}
		});
		txtAmount.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					saveButton.doClick();
				}
			}
		});

		cboxType = new JComboBox<String>();
		cboxType.getEditor().getEditorComponent().addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				cboxType.getEditor().selectAll();
			}
		});
		cboxType.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					// addType cboxType.getSelectedItem();
					saveButton.doClick();
				}
			}
		});
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.insets = new Insets(0, 0, 0, 5);
		gbc_comboBox.gridx = 4;
		gbc_comboBox.gridy = 0;
		add(cboxType, gbc_comboBox);

		cboxType.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					saveButton.doClick();
				}
			}
		});

		txtTags = new JTextField();
		txtTags.setForeground(Color.LIGHT_GRAY);
		txtTags.setText("insert tags ");
		GridBagConstraints gbc_txtTags = new GridBagConstraints();
		gbc_txtTags.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTags.anchor = GridBagConstraints.NORTH;
		gbc_txtTags.insets = new Insets(0, 0, 0, 5);
		gbc_txtTags.gridx = 5;
		gbc_txtTags.gridy = 0;
		add(txtTags, gbc_txtTags);
		txtTags.setColumns(13);
		txtTags.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				txtTags.setForeground(Color.BLACK);
				if (txtTags.getText().equals("insert tags")) {
					txtTags.setText("");
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				if (txtTags.getText().equals("")) {
					txtTags.setForeground(Color.LIGHT_GRAY);
					txtTags.setText("insert tags");
				}
			}
		});
		txtTags.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					saveButton.doClick();
				}
			}
		});

		dateChooser = new JDateChooser();
		dateChooser.getComponent(0).setVisible(false);
		dateChooser.getComponent(1).addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				dateChooser.getComponent(0).setVisible(true);
				((JButton) dateChooser.getComponent(0)).doClick();
				dateChooser.getComponent(0).setVisible(false);
			}
		});
		dateChooser.getComponent(1).addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					saveButton.doClick();
				}
			}
		});
		dateChooser.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		dateChooser.setDateFormatString("dd MMM yyyy");
		GridBagConstraints gbc_dateChooser = new GridBagConstraints();
		gbc_dateChooser.insets = new Insets(0, 0, 0, 5);
		gbc_dateChooser.gridx = 6;
		gbc_dateChooser.gridy = 0;
		add(dateChooser, gbc_dateChooser);

		btnX = new JButton("<html><body><img src=\"file:assets/DeleteRed.png\"  /></body></html>");
		GridBagConstraints gbc_btnX = new GridBagConstraints();
		gbc_btnX.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnX.insets = new Insets(0, 0, 0, 5);
		gbc_btnX.anchor = GridBagConstraints.NORTH;
		gbc_btnX.gridx = 7;
		gbc_btnX.gridy = 0;
		add(btnX, gbc_btnX);

		saveButton = new JButton("<html><body><img src=\"file:assets/save_icon.gif\"  /></body></html>");
		GridBagConstraints gbc_saveButton = new GridBagConstraints();
		gbc_saveButton.fill = GridBagConstraints.HORIZONTAL;
		gbc_saveButton.gridx = 8;
		gbc_saveButton.gridy = 0;
		add(saveButton, gbc_saveButton);
		// saveButton.setVisible(false);
	}

	public void setDeletable(boolean flag) {
		btnX.setEnabled(flag);
	}

	public void setIndex(int newNum) {
		expenseNum.setText(Integer.toString(newNum));
	}

	public void setId(UUID newID) {
		id = newID;
	}

	public UUID getId() {
		return id;
	}

	public String getItemName() {
		return txtName.getText();
	}

	public void setItemName(String name) {
		txtName.setForeground(Color.BLACK);
		txtName.setText(name);
	}

	public void setAvailableTypes(String[] types) {
		cboxType.setModel(new DefaultComboBoxModel<String>(types));
		cboxType.setSelectedItem(type);
	}

	public String getType() {
		type = (String) cboxType.getSelectedItem();
		return type;
	}

	public void setType(String type) {
		cboxType.setSelectedItem(type);
		this.type = type;
	}

	public String getAmount() {
		return txtAmount.getText();
	}

	public void setAmount(String amount) {
		txtAmount.setForeground(Color.BLACK);
		txtAmount.setText(amount);
	}

	public String getTags() {
		String text = txtTags.getText();
		if (text.equals("insert tags"))
			return "";
		else
			return text;
	}

	public void setTags(String tags) {
		if (tags.isEmpty()) {
			txtTags.setForeground(Color.LIGHT_GRAY);
			txtTags.setText("insert tags");
		} else {
			txtTags.setText(tags);
			txtTags.setForeground(Color.BLACK);
		}
	}

	public void setDate(Date date) {
		// dateChooser.setDate(date);
		dateChooser.setDate(date);
	}

	public Date getDate() {
		return dateChooser.getDate();
	}

	public void clearFields() {
		txtName.setForeground(Color.LIGHT_GRAY);
		txtName.setText("name ");
		txtAmount.setForeground(Color.LIGHT_GRAY);
		txtAmount.setText("price");
		txtTags.setForeground(Color.LIGHT_GRAY);
		txtTags.setText("insert tags ");
		type = "Other";
		cboxType.setSelectedItem(type);
	}

	public void addSaveHandler(EventHandler<BudgetItem> handler) {
		saveButton.addActionListener(new ItemListener(handler));
	}

	public void addDeleteHandler(EventHandler<BudgetItem> handler) {
		btnX.addActionListener(new ItemListener(handler));
	}

	public void setNewFocus() {
		txtName.requestFocus();
	}

	private class ItemListener implements ActionListener {
		public ItemListener(EventHandler<BudgetItem> handler) {
			this.handler = handler;
		}

		@Override
		public void actionPerformed(final ActionEvent arg0) {
			handler.handle(BudgetItem.this);
		}

		private EventHandler<BudgetItem> handler;
	}

	private static final long serialVersionUID = 1L;

	private String type = "Other";
	private JDateChooser dateChooser;
	private UUID id;
	private JLabel expenseNum;
	private JTextField txtName;
	private JTextField txtAmount;
	private JTextField txtTags;
	private JComboBox<String> cboxType;
	private JButton btnX;
	private JButton saveButton;
	protected JLabel lblNewLabel;

}
